﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using AjaxChatWebApp.DTO;

namespace AjaxChatWebApp.Account
{
    public partial class Manage : System.Web.UI.Page
    {
        HttpClient httpClient = new HttpClient();
        protected void Page_Load()
        {
            httpClient.BaseAddress = new Uri("http://localhost:62865");
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
        /*
        protected void setPassword_Click(object sender, EventArgs e)
        {
            
        }


        protected void externalLoginsList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            
        }


        protected static string ConvertToDisplayDateTime(DateTime? utcDateTime)
        {
            // You can change this method to convert the UTC date time into the desired display
            // offset and format. Here we're converting it to the server timezone and formatting
            // as a short date and a long time string, using the current thread culture.
            return utcDateTime.HasValue ? utcDateTime.Value.ToLocalTime().ToString("G") : "[never]";
        }
        */
        protected void btUpdate_Click(object sender, EventArgs e)
        {
            //string userName = Session["UserName"].ToString();
            int id = (int)Session["UserId"];
            var user = GetUser(id);
            if (user == null)
                return;

            if (tbFirstName.Text != "")
                user.FirstName = tbFirstName.Text;

            if (tbLastName.Text != "")
                user.LastName = tbLastName.Text;

            if(tbCurrentPassword.Text != "" && tbNewPassword.Text != "" && ConfirmNewPassword.Text != "")
            {
                string currentPwd = tbCurrentPassword.Text;
                string newPwd = tbNewPassword.Text;

                if (user.Password.Equals(currentPwd))
                {
                    user.Password = newPwd;
                    UpdateUser(user);
                }
                else
                {
                    ErrorMessage.Text = "Wrong password";
                }
            }
          
            if(fuAvatar.HasFile)
            {
                UploadAvatar();
                user.Avatar = fuAvatar.FileName;
                UpdateUser(user);
            }
        }

        private User GetUser(int id)
        {
            User user = null;
            HttpResponseMessage response = httpClient.GetAsync("api/users/" + id).Result;
            if (response.IsSuccessStatusCode)
            {
                user = response.Content.ReadAsAsync<User>().Result;
            }
            return user;
        }

        private void UpdateUser(User user)
        {
            HttpResponseMessage response = httpClient.PutAsJsonAsync("api/users/" + user.Id, user).Result;
            if (response.IsSuccessStatusCode)
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        private void UploadAvatar()
        {
            if(fuAvatar.HasFile)
            {
                string desPath = Server.MapPath("~/Avatars/" + fuAvatar.FileName);
                fuAvatar.SaveAs(desPath);
            }
        }
    }
}